package com.example.sensortest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;


public class MainActivity extends Activity implements SensorEventListener {

	private TextView tv;
	private SensorManager sManager;
	
    private float[] gyro_data = {0, 0, 0}, acc_data = {0, 0, 0};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        
        //get the TextView from the layout file
        tv = (TextView) findViewById(R.id.tv);
        
        //get a hook to the sensor service
        sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	protected void onResume()
	{
		super.onResume();

		sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
		sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	protected void onStop()
	{
		sManager.unregisterListener(this);
		super.onStop();
	}
	
	public void onAccuracyChanged(Sensor arg0, int arg1)
	{
		// Do nothing.
	}
	
	public void onSensorChanged(SensorEvent event)
	{
		//if sensor is unreliable, return void
		if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
			return; 
              
		
		tv.setText("Sensor-type :"+ Integer.toString(event.sensor.getType()) +"\n"+
				   "Sensor-Data-0 :"+ Float.toString(event.values[0]) +"\n"+
				   "Sensor-Data-1 :"+ Float.toString(event.values[1]) +"\n"+
				   "Sensor-Data-2 :"+ Float.toString(event.values[2]));
	}
	
}
